package com.Jano.service.impl;

import com.Jano.entity.Invitation;
import com.Jano.exception.BusinessException;
import com.Jano.mapper.InvitationMapper;
import com.Jano.mapper.ReplyMapper;
import com.Jano.service.InvitationService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.List;
import java.util.Objects;

/**
 * description: InvitationServiceImpl <br>
 * List<Invitation> invitations = invitationMapper.selectAllInvitation();
 *  PageInfo<Invitation> pageInfo =PageInfo.of(invitations);
 * date: 2022/7/12 1:53 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */
@Service
public class InvitationServiceImpl extends AbstractBaseServiceImpl implements InvitationService {

    @Autowired
    InvitationMapper invitationMapper;

    @Autowired
    ReplyMapper replyMapper;

    @Override
    public PageInfo<Invitation> findInvitationPagination(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize,"createdate desc");
        List<Invitation> invitations = invitationMapper.selectAllInvitation();
        PageInfo<Invitation> pageInfo = PageInfo.of(invitations);
        return pageInfo;
    }

    @Override
    public PageInfo<Invitation> findInvitationByName(Integer pageNum, Integer pageSize,String title) {
        PageHelper.startPage(pageNum, pageSize,"createdate desc");
        List<Invitation> invitations = invitationMapper.selectInvitationByName(title);
        PageInfo<Invitation> pageInfo =PageInfo.of(invitations);
        return pageInfo;
    }

    /**
     * Spring boot是默认启动事务的,只需要在(service层中添加)类或者方法上添加@Transactional注解即可
     * @param invitationId
     * @return
     */
    @Override
    @Transactional
    public Boolean deleteInvitationById(Integer invitationId) {
        throwIfNull(invitationId,"参数不能为null");
        Boolean booleanOne = invitationMapper.deleteInvitationById(invitationId);
        Boolean booleanTwo = replyMapper.deleteReply(invitationId);
        return booleanOne||booleanTwo;
    }
}
